#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# Project: blogsite
#  Author: an00
#  Create: 2018/4/5 17:45


from django import template
from django.db.models.aggregates import Count
from blog.models import Article, Tag, Category


register = template.Library()


@register.simple_tag
def get_newest_arts(num=5):
    return Article.objects.all()[:num]


@register.simple_tag
def get_time_archives():
    return Article.objects.dates('create_time', 'month', order='DESC')


@register.simple_tag
def get_categories():
    return Category.objects.annotate(num_arts=Count('article')).filter(num_arts__gt=0)


@register.simple_tag
def get_tags():
    return Tag.objects.annotate(num_arts=Count('article')).filter(num_arts__gt=0)
